package com.codetree.strategy.functionalInterface.strategy;

import com.codetree.strategy.common.Constant;
import com.codetree.strategy.functionalInterface.PaymentFunctionStrategy;
import com.codetree.strategy.functionalInterface.domain.PaymentResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Slf4j
@Configuration
public class AlipayConfig {

    @Value("${payment.alipay.app-id}")
    private String appId;

    @Value("${payment.alipay.private-key}")
    private String privateKey;


    @Bean(Constant.PAYConstant.ALI_PAY)
    public PaymentFunctionStrategy alipayStrategy() {
        return amount -> {
            // 模拟真实支付宝调用逻辑
            try {
                // 1. 构造请求参数
                Map<String, String> params = new HashMap<>();
                params.put("app_id", appId);
                params.put("amount", amount.toString());
                params.put("sign", generateSignature(privateKey, amount));
                log.info("Alipay Payment Sign: {}", params);

                // 2. 调用支付宝API（示例伪代码）
                String response = "{\"success\":true,\"transactionId\":\"11111\",\"message\":\"支付成功\"}";

                log.info("支付成功:{}", response);
            } catch (Exception e) {
                throw new RuntimeException("支付宝系统异常: ", e);
            }
            return new PaymentResult(Boolean.TRUE, UUID.randomUUID().toString(), "支付宝支付成功");
        };
    }

    private String generateSignature(String key, BigDecimal amount) {
        // 签名生成逻辑（示例）
        return "SIGN_" + key.hashCode() + "_" + amount;
    }

}
